#include <cstdio>
#include <numeric>
#include <vector>
constexpr int MaxN = 5e5 + 5;
int n;
int answer;
int fa[MaxN];
bool vis[MaxN];
std::vector<int> g[MaxN];
int find(int u)
{
    return fa[u] == u ? u : fa[u] = find(fa[u]);
}
void merge(int u, int v)
{
    int fu = find(u);
    int fv = find(v);
    if (fu != fv)
    {
        fa[fv] = fu;
    }
}
bool dfs(int u, int f)
{
    if (vis[u])
    {
        return true;
    }
    vis[u] = true;
    for (const auto &v : g[u])
    {
        if (v == f)
        {
            continue;
        }
        if (dfs(v, u))
        {
            return true;
        }
    }
    return false;
}
int main()
{
    freopen("pairint.in", "r", stdin);
    freopen("pairint.out", "w", stdout);
    scanf("%d", &n);
    std::iota(fa + 1, fa + 1 + 2 * n, 1);
    for (int i = 1; i <= n; i++)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
        merge(u, v);
    }
    answer = 2 * n;
    for (int i = 1; i <= 2 * n; i++)
    {
        if (vis[i])
        {
            continue;
        }
        answer -= dfs(i, i) ? 0 : 1;
    }
    printf("%d\n", answer);
    return 0;
}